package com.google.code.tretris.events;

import android.os.Parcel;
import android.os.Parcelable;

public class MoveEvent implements Parcelable {
	
	
	private final int deltax;
	private final int rotationchange;
	private final String userId;
	private static MoveEvent right;
	private static MoveEvent left;
	private static MoveEvent rotate;

	public MoveEvent(final String userId, final int deltax, final int rotationchange) {
		this.userId = userId;
		this.deltax = deltax;
		this.rotationchange = rotationchange;
	}
	
	public static final MoveEvent right(final String userId) {
		if (right == null) {
			right = new MoveEvent(userId, 1, 0);
		} 
		return right;
	}

	public static final MoveEvent left(final String userId) {
		if (left == null) {
			left = new MoveEvent(userId, -1, 0);
		} 
		return left;
	}
	
	public static final MoveEvent rotate(final String userId) {
		if (rotate == null) {
			rotate = new MoveEvent(userId, 0, 1);
		} 
		return rotate;
	}
	
	public String getUserId() {
		return userId;
	}
	
	public int getDeltax() {
		return deltax;
	}

	public int getRotationchange() {
		return rotationchange;
	}

	@Override
	public String toString() {
		return "Event [rotationchange: " + rotationchange + ", deltax: " + deltax + "]" ;
	}

	public static final Parcelable.Creator<MoveEvent> CREATOR = new Creator<MoveEvent>() {
		
		public MoveEvent[] newArray(int size) {
			return new MoveEvent [size] ;
		}
		
		public MoveEvent  createFromParcel(Parcel source) {
			final String userId = source.readString();
			final int x = source.readInt();
			final int rotation = source.readInt();
			
			return new MoveEvent(userId, x, rotation);
		}
	};
	
	public int describeContents() {
		return 0;
	}

	public void writeToParcel(Parcel dest, int flags) {
		dest.writeString(userId) ;
		dest.writeInt(deltax) ;
		dest.writeInt(rotationchange);
	}
	
}
